<?php

/**
 * Contains a class to display /admin/events.php.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @author   Spencer Hakim <hakims@rpi.edu>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
 
/** @uses paginate To create pagination links. */
require_once 'function_paginate.php';

/** @uses valueOf For safe handling of arrays. */
require_once 'function_valueof.php';
 
/**
 * A class to display /admin/events.php.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @author   Spencer Hakim <hakims@rpi.edu>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class PageAdminEvents extends Page
{
    /**
     * The current page view.
     *
     * @access public
     * @var int
     */
    public $page;
    
    /**
     * A variable to contain an error message to be displayed.
     *
     * @access public
     * @var string
     */
    public $error;
    
    /**
     * Constructor function.  Calls the parent constructor, sets the title.
     */
    public function __construct()
    {
        parent::__construct();
        $this->page = valueOf($_GET, 'page');
        if ($this->page < 1) {
            $this->page = 1;
        }
        parent::setTitle('Manage Events');
    }

    /**
     * Function to return the HTML to print the pagination links.
     *
     * @access private
     * @return string  The HTML containing the pagination links.
     */
    private function _getPagination()
    {
        $total = $this->db->getNumberOfEvents();
        return paginate($this->page, $total, EVENTS_PER_PAGE);
    }
    
    /**
     * Function to print the list of stores matching the category criteria.
     *
     * @access private
     * @return null
     */
    private function _printEvents()
    {
        if ($this->error != '') {
            printError($this->error, 1);
        }
        $pagination = $this->_getPagination();
        $events     = $this->db->getEvents($this->page, null, null);
        echo $pagination,
           ' <ul class="stores">', "\n";
        $countries = $this->db->getCountries();
        foreach ($events as $event) {
            $event->country = $countries[$event->country - 1]['name'];
            $event->printListItemFull(true);
        }
        echo ' </ul>', "\n",
            $pagination, "\n";
    }
    
    /**
     * Function to display the page.
     *
     * @access public
     * @return null
     */
    public function display()
    {
        parent::printHeader();
        if ($this->user->role == R_ADMIN) {
            if (valueOf($_GET, 'action') == 'delete') {
                $result = $this->db->deleteEvent(
                    new Event((int)valueOf($_GET, 'eventid'))
                );
                if ($result !== true) {
                    $this->error = $result;
                }
            }
            
            $this->_printEvents();
            
        } else {
            echo "You do not have permission to access this page.";
        }
        parent::printFooter();
    }
}

?>